﻿Public Class NEWUSER
    Dim connect As CONDB = CONDB.NewConnection()

    '-----------filter sub------------------
    Public Sub filterPrename()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(a.PRENAME AS CHAR(8)) AS PRENAME, a.FTPRENAME, b.SEXDESC FROM masprename a LEFT JOIN massex b ON a.sex = b.SEX WHERE a.STATUS = '1'"
        s = New FILTERCLASS(prenameTxt, sql, "id,คำนำหน้าชื่อ,เพศ", "50,160,50", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterSex()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(sex AS CHAR(8)) as sex, sexdesc FROM massex WHERE `status` = '1'"
        s = New FILTERCLASS(sexTxt, sql, "id เพศ,เพศ", "50,120", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    '--------clear after click---------
    Private Sub prenameTxt_Click(sender As Object, e As EventArgs) Handles prenameTxt.Click
        prenameTxt.Clear()
    End Sub

    Private Sub sexTxt_Click(sender As Object, e As EventArgs) Handles sexTxt.Click
        sexTxt.Clear()
    End Sub

    Public Sub clearUserForm()
        prenameTxt.Clear()
        nameTxt.Clear()
        LnameTxt.Clear()
        nNameTxt.Clear()
        name_enTxt.Clear()
        sexTxt.Clear()
        CIDTxt.Clear()
        empCodeTxt.Clear()
        usreqTxt.Clear()
        confirmUsreqTxt.Clear()
        passwordMatch.Visible = False
        hospempStatusChk.Checked = False
        f_cashStatusChk.Checked = False
    End Sub

    Private Sub NEWUSER_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        filterPrename()
        filterSex()
        addHospemp.Enabled = False
    End Sub

    '--------check password----------
    Private Sub UMAINPASSTxt_TextChanged(sender As Object, e As EventArgs) Handles usreqTxt.TextChanged
        If usreqTxt.Text = confirmUsreqTxt.Text Then
            passwordMatch.Visible = False
            addHospemp.Enabled = True
        Else
            passwordMatch.Visible = True
            addHospemp.Enabled = False
        End If
    End Sub


    Private Sub CONFIRMPASSTxt_TextChanged(sender As Object, e As EventArgs) Handles confirmUsreqTxt.TextChanged
        If usreqTxt.Text = confirmUsreqTxt.Text Then
            passwordMatch.Visible = False
            addHospemp.Enabled = True
        Else
            passwordMatch.Visible = True
            addHospemp.Enabled = False
        End If
    End Sub

    Private Sub addHospemp_Click(sender As Object, e As EventArgs) Handles addHospemp.Click
        If prenameTxt.Text.ToString.Length > 0 And nameTxt.Text.ToString.Length > 0 And LnameTxt.Text.Length.ToString > 0 And nNameTxt.Text.Length.ToString > 0 And usreqTxt.Text.Length.ToString > 0 And confirmUsreqTxt.Text.Length.ToString > 0 Then
            Dim objInsertData As New USERGROUPCLASS
            objInsertData.CID_ = CIDTxt.Text

            If empCodeTxt.Text Is Nothing Or empCodeTxt.Text = "" Then
                objInsertData.EMPCODE_ = 0
            Else
                objInsertData.EMPCODE_ = CInt(empCodeTxt.Text)
            End If

            objInsertData.PRENAME_ = prenameTxt.Tag
            objInsertData.NAME_ = nameTxt.Text
            objInsertData.LNAME_ = LnameTxt.Text
            objInsertData.NNAME_ = nNameTxt.Text
            objInsertData.NAME_EN_ = name_enTxt.Text
            objInsertData.SEX_ = sexTxt.Tag
            objInsertData.F_CASH_ = f_cashStatusChk.Checked
            objInsertData.USREQ_ = usreqTxt.Text
            objInsertData.STATUS_ = hospempStatusChk.Checked

            If objInsertData.insertUser() = True Then
                MsgBox("ท่านได้ทำการเพิ่มผู้ใช้เรียบร้อยแล้ว")
                DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            End If
            connect.Dispose()
        Else
            MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนทุกช่องก่อนทำการเพิ่มข้อมูล")
        End If
    End Sub
End Class